package com.linq.cool.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linq.cool.common.constants.Constants;
import com.linq.cool.common.core.utils.Convert;
import com.linq.cool.common.core.utils.StringUtils;
import com.linq.cool.common.redis.service.RedisService;
import com.linq.cool.system.domain.SysConfig;
import com.linq.cool.system.dto.SysConfigQueryDTO;
import com.linq.cool.system.mapper.SysConfigMapper;
import com.linq.cool.system.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2020/12/29 15:11
 * @Description:
 * @Version: 1.0.0
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    @Autowired
    private RedisService redisService;


    @Override
    public IPage<SysConfig> listByPage(IPage<SysConfig> page, SysConfigQueryDTO dto) {
        return this.page(page, new LambdaQueryWrapper<SysConfig>()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.checkValNotNull(dto.getName()), SysConfig::getName, dto.getName())
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.checkValNotNull(dto.getKey()), SysConfig::getKey, dto.getKey())
                .eq(com.baomidou.mybatisplus.core.toolkit.StringUtils.checkValNotNull(dto.getType()), SysConfig::getType, dto.getType())
                .between(com.baomidou.mybatisplus.core.toolkit.StringUtils.checkValNotNull(dto.getParams().get(Constants.BEGIN_TIME)), SysConfig::getGmtCreate, dto.getParams().get("beginTime"), dto.getParams().get(Constants.END_TIME))
                .orderByAsc(SysConfig::getGmtCreate)
        );
    }

    @Override
    public String getByKey(String configKey) {
        // 从redis中拿出配置信息
        String configValue = Convert.toStr(redisService.getCacheObject(getCacheKey(configKey)));
        if (StringUtils.isNotEmpty(configValue)) {
            return configValue;
        }
        // 根据 参数键名 查询当前配置信息
        SysConfig one = getOne(new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getKey, configKey));
        if (StringUtils.isNotNull(one)) {
            redisService.setCacheObject(getCacheKey(configKey), one.getValue());
            return one.getValue();
        }
        return StringUtils.EMPTY;
    }

    @Override
    public boolean checkKeyUnique(SysConfig config) {
        // 判断传入的是否是空(新增或者更细) 新增的时候id roleId=-1  更新就是原来的
        Long configId = null == config.getId() ? -1L : config.getId();
        SysConfig one = this.getOne(new LambdaQueryWrapper<SysConfig>()
                                            .eq(SysConfig::getKey, config.getKey())
        );
        // 判断id是否一致 不一致就代表 唯一 false
        return null != one && !configId.equals(one.getId());
    }

    @Override
    public boolean saveConfig(SysConfig config) {
        boolean flag = this.save(config);
        if (flag) {
            // 重新存入数据库
            redisService.setCacheObject(getCacheKey(config.getKey()), config.getValue());
        }
        return flag;
    }

    @Override
    public boolean updateConfig(SysConfig config) {
        boolean flag = this.updateById(config);
        if (flag) {
            // 重新存入数据库
            redisService.setCacheObject(getCacheKey(config.getKey()), config.getValue());
        }
        return flag;
    }

    @Override
    public boolean deleteByIds(List<Long> configIds) {
        boolean flag = this.removeByIds(configIds);
        if (flag) {
            // 拿到所有 配置信息
            Collection<String> keys = redisService.keys(Constants.SYS_CONFIG_KEY + "*");
            // 删除所有
            redisService.deleteObject(keys);
        }
        return flag;
    }

    @Override
    public void clearCache() {
        Collection<String> keys = redisService.keys(Constants.SYS_CONFIG_KEY + "*");
        redisService.deleteObject(keys);
    }

    @Override
    public List<SysConfig> listByParams(SysConfig config) {
        return this.list(this.getLambadaQueryWrapper(config));
    }

    private LambdaQueryWrapper<SysConfig> getLambadaQueryWrapper(SysConfig config) {
        return new LambdaQueryWrapper<SysConfig>()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.checkValNotNull(config.getName()), SysConfig::getName, config.getName())
                .eq(com.baomidou.mybatisplus.core.toolkit.StringUtils.checkValNotNull(config.getType()), SysConfig::getName, config.getType())
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.checkValNotNull(config.getKey()), SysConfig::getName, config.getKey())
                ;
    }

    /**
     * 设置cache key
     *
     * @param key 参数键
     *
     * @return 缓存键key
     */
    private String getCacheKey(String key) {
        return Constants.SYS_CONFIG_KEY + key;
    }
}
